/*
 * Copyright (C) 2019-2020 Alibaba Group Holding Limited
 */

#define BT_DBG_ENABLED 1

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <aos/aos.h>
#include <aos/kernel.h>

#include <aos/hal/adc.h>
#include <ulog/ulog.h>
#include <yoc/atserver.h>
#include "pin_config.h"

#ifdef TEST_ADC
#define TAG "TEST_HAL_ADC"
/* define dev */
adc_dev_t adc1;

int aos_hal_adc_api_test()
{
    int32_t ret1 = -1;
    int32_t ret2 = -1;
    int32_t ret3 = -1;
    int32_t ret4 = -1;
    int32_t ret5 = -1;
    uint32_t value = 0;

    /* adc port set */
    adc1.port = ADC1_PORT_NUM;

    /* set sampling_cycle */
    adc1.config.sampling_cycle = 100;

    /* init adc1 with the given settings */
    ret1 = hal_adc_init(NULL);

    ret2 = hal_adc_init(&adc1);

    ret3 = hal_adc_value_get(NULL, &value, HAL_WAIT_FOREVER);

    ret4 = hal_adc_value_get(&adc1, NULL, HAL_WAIT_FOREVER);

    /* finalize adc1 */

    ret5 = hal_adc_finalize(&adc1);

    printf("%d \n%d \n%d \n%d \n%d \n",ret1,ret2,ret3,ret4,ret5);
    if (ret1 != 0 && ret2 == 0 && ret3 != 0 && ret4 != 0 && ret5 == 0) {
            AT_BACK_OK();
    }
    else{
            AT_BACK_ERR();
    }
    return 0;
}

int aos_hal_adc_test()
{
    int ret   = -1;
    uint32_t value = 0;
    int i = 0;

    /* adc port set */
    adc1.port = ADC1_PORT_NUM;

    /* set sampling_cycle */
    adc1.config.sampling_cycle = 100;

    /* init adc1 with the given settings */
    ret = hal_adc_init(NULL);

    ret = hal_adc_init(&adc1);

    if (ret != 0) {
        AT_BACK_ERR();
    }

    for (i=0; i<100; i++){
        /* get adc value */
        ret = hal_adc_value_get(&adc1, &value, HAL_WAIT_FOREVER);

        if (ret != 0) {
            AT_BACK_ERR();
        }
        else{
            if (value!=0)
                AT_BACK_ERR();
        }    
    }

    ret = hal_adc_value_get(&adc1, &value,0);
    if (value!=0)
        AT_BACK_ERR();

    /* finalize adc1 */

    hal_adc_finalize(&adc1);
    ret = hal_adc_value_get(&adc1, &value,0);
    AT_BACK_OK();
    return 0;
}

int aos_hal_adc_read_test()
{
    int ret   = -1;
    uint32_t value = 0;
    int i = 0;

    /* adc port set */
    adc1.port = ADC1_PORT_NUM;

    /* set sampling_cycle */
    adc1.config.sampling_cycle = 100;

    /* init adc1 with the given settings */
    ret = hal_adc_init(NULL);

    ret = hal_adc_init(&adc1);

    if (ret != 0) {
        AT_BACK_ERR();
    }

    for (i=0; i<100; i++){
        /* get adc value */
        ret = hal_adc_value_get(&adc1, &value, HAL_WAIT_FOREVER);

        if (ret != 0) {
            AT_BACK_ERR();
        }
        else{
            printf("adc data is = %d \n",value);
        }    
    }
    /* finalize adc1 */

    hal_adc_finalize(&adc1);
    AT_BACK_OK();
    return 0;
}
    
void test_hal_adc(char *cmd, int type, char *data)
{
    if (strcmp((const char *)data, "'READ'\0") == 0) {
        aos_hal_adc_read_test();
    }else if (strcmp((const char *)data, "'API'\0") == 0) {
        aos_hal_adc_api_test();
    }else if (strcmp((const char *)data, "'TEST'\0") == 0) {
        aos_hal_adc_test();
    }    
}
#endif